from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSBase as GSResults

from GSTargetInfoPolicies import GSSinglePlotTarget
from GSTargetInfoPolicies import GSSpecificBuildTarget

from GSGraphicsPolicies import GSPlot
from GSGraphicsPolicies import GSPieMenu

gc = CyGlobalContext()

class RemoteImprovement(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)

        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        pBuildTargetPolicy = GSSpecificBuildTarget(self.canTargetBuild)

        stageList = [
            (pPlotTargetPolicy, GSPlot()),
            (pBuildTargetPolicy, GSPieMenu())
            ]
        
        GSHandler.__init__(self, stageList)
        
        GSResults.__init__(self) # Uses base class

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if (pCastingUnit.getUnitType() == gc.getInfoTypeForString("UNIT_WORKER")):
            return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pPotentialPlot, bVisible):
        return True

    def canTargetBuild(self, pCastingGroup, pCastingPlot, iPotentialBuild, bVisible):
        pPlot = self.getTargettingResults(0).getTarget()
        if (pCastingGroup.canStartMission(MissionTypes.MISSION_BUILD, iPotentialBuild, -1, pPlot, bVisible)):
            return True

    def castSpell(self, pTargetInfo):
        pPlotList = self.getTargettingResults(0).getAffectedTargetList(self.getCastingGroup(), self.getCastingPlot())
        for pPlot in pPlotList:
            gc.getGame().selectionListGameNetMessage(GameMessageTypes.GAMEMESSAGE_PUSH_MISSION, int(MissionTypes.MISSION_MOVE_TO), pPlot.getX(), pPlot.getY(), 0, 0, 0 )
            gc.getGame().selectionListGameNetMessage(GameMessageTypes.GAMEMESSAGE_PUSH_MISSION, int(MissionTypes.MISSION_BUILD),   pTargetInfo.getData1(), pTargetInfo.getData2(), 0, 0, 1 )
